package com.itany.controllers;

import com.itany.entity.User;
import com.itany.service.ManagerUserService;
import com.itany.service.UserService;
import com.itany.vo.ActionResult;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService service;

    //查找所有用户的信息
    @RequestMapping("/findAll")
    public Map<String, Object> findAll(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows,
            @RequestParam("name") String name,
            @RequestParam("phone") String phone,
            @RequestParam("wechat") String wechat,
            @RequestParam("qq") String qq,
            @RequestParam("type") Integer type,
            @RequestParam("companyid") String companyid) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = service.queryByParams(name, phone, wechat, qq, type, companyid);
        int total = list != null ? list.size() : 0;

        // 如果总数据量小于或等于每页显示的数据条数，则始终从第一页开始，且只显示一页
        if (total <= rows) {
            page = 1;
        } else {
            // 计算总页数，并检查请求的页码是否超出范围
            int totalPages = (int) Math.ceil((double) total / rows);
            if (page > totalPages) {
                page = 1; // 如果请求的页码大于总页数，则重置为第一页
            }
        }

        // 计算起始和结束索引
        int startIndex = (page - 1) * rows;
        int endIndex = Math.min(startIndex + rows, total);

        // 创建结果列表，并填充数据
        if(list!=null){
            List<Map<String, Object>> ref = list.subList(startIndex, endIndex);
            map.put("rows", ref);
        }else{
            map.put("rows",0);
        }

        map.put("total", total);

        return map;
    }

    //查看操作，获取对应行用户的信息
    @RequestMapping("/findById")
    public ActionResult findById(@RequestParam("id") Integer id){
        Map<String,Object> map = service.findById(id);
        return ActionResult.success("查找成功",200,map);
    }

    @RequestMapping("/modifyStatus")
    public ActionResult modifyStatus(@RequestParam("id") Integer id){
        service.modifyStatus(id);
        return ActionResult.success("修改状态成功",200,null);
    }

    @RequestMapping("/findByCompanyId")
    public Map<String, Object> findByCompanyId(
            @RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        if(id != -1){
            List<Map<String, Object>> list = service.queryByCompanyId(id);
            int total = list != null ? list.size() : 0;
            if(list != null){
                map.put("rows", list);
            }else{
                map.put("rows", 0);
            }
            map.put("total", total);
        }else{
            map.put("rows", 0);
            map.put("total", 0);
        }
        return map;
    }

    @RequestMapping("/modifyCompanyId")
    public ActionResult modifyCompanyId(@RequestParam("id") Integer id) {
        service.modifyCompanyId(id);
        return ActionResult.success("修改所属公司成功",200,null);
    }

    @RequestMapping("/add")
    public ActionResult add(
            @RequestParam("phone") String phone,
            @RequestParam("companyid") Integer companyid) {
        ActionResult actionResult = service.addCompanyId(phone,companyid);
        return actionResult;
    }
}
